package siau.android.base

import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.TypedValue
import android.view.*
import android.widget.*
import androidx.core.content.ContextCompat
import hhm.android.library.utils.GetStringListener
import hhm.android.library.utils.GlideUtils
import siau.android.http.model.AdvertisementModel


fun getStringDialog(
    context: Context, titleStr: String, defaultStr: String, listener: GetStringListener
) {
    val view = LayoutInflater.from(context).inflate(R.layout.dialog_get_string, null, false)
    val dialog = AlertDialog.Builder(context, R.style.dialog_radius_10).setView(view).setCancelable(true)
    val show = dialog.show()
    setDialogWindowAttr(show, context, 300f, 150f)
    val title = view.findViewById<TextView>(R.id.dialog_get_string_title)
    title.text = titleStr
    val cancel = view.findViewById<TextView>(R.id.dialog_get_string_cancel)
    val sure = view.findViewById<TextView>(R.id.dialog_get_string_sure)
    val et = view.findViewById<EditText>(R.id.dialog_get_string_et)
    if (defaultStr.isNotEmpty()) et.setText(defaultStr)
    cancel.setOnClickListener { show.dismiss() }
    sure.setOnClickListener {
        if (et.text.trim().isNotEmpty()) {
            show.dismiss()
            listener.getString(et.text.toString())
        }
    }
}

fun getSureDialog(
    context: Context, listener: View.OnClickListener, cancelListener: View.OnClickListener? = null, title: String = "", content: String = "", showTitle: Boolean = true, showContent: Boolean = true
) {
    val view = LayoutInflater.from(context).inflate(R.layout.dialog_sure, null, false)
    val dialog = AlertDialog.Builder(context, R.style.dialog_radius_10).setView(view).setCancelable(true)
    val show = dialog.show()
    setDialogWindowAttr(show, context, 300f, 150f)
    val cancel = view.findViewById<TextView>(R.id.dialog_sure_cancel)
    val sure = view.findViewById<TextView>(R.id.dialog_sure_sure)
    val titleView = view.findViewById<TextView>(R.id.dialog_sure_title)
    titleView.visibility = if (showTitle) {
        View.VISIBLE
    } else {
        View.GONE
    }
    val contentView = view.findViewById<TextView>(R.id.dialog_sure_content)
    contentView.visibility = if (showContent) {
        View.VISIBLE
    } else {
        View.GONE
    }
    titleView.text = title
    contentView.text = content
    cancel.setOnClickListener {
        cancelListener?.onClick(it)
        show.dismiss()
    }
    sure.setOnClickListener {
        show.dismiss()
        listener.onClick(it)
    }
}


fun getTipDialog(
    context: Context, listener: View.OnClickListener, title: String = "", content: String = "", showTitle: Boolean = true, showContent: Boolean = true
) {
    val view = LayoutInflater.from(context).inflate(R.layout.dialog_sure, null, false)
    val dialog = AlertDialog.Builder(context).setView(view).setCancelable(true)
    val show = dialog.show()
    val cancel = view.findViewById<TextView>(R.id.dialog_sure_cancel)
    val vLine = view.findViewById<View>(R.id.dialog_sure_vertical_line)
    cancel.visibility = View.GONE
    vLine.visibility = View.GONE
    val hLine = view.findViewById<View>(R.id.dialog_sure_horizontal_line)
    hLine.visibility = View.VISIBLE
    val sure = view.findViewById<TextView>(R.id.dialog_sure_sure)
    val titleView = view.findViewById<TextView>(R.id.dialog_sure_title)
    titleView.visibility = if (showTitle) {
        View.VISIBLE
    } else {
        View.GONE
    }
    val contentView = view.findViewById<TextView>(R.id.dialog_sure_content)
    contentView.visibility = if (showContent) {
        View.VISIBLE
    } else {
        View.GONE
    }
    titleView.text = title
    contentView.text = content
    cancel.setOnClickListener { show.dismiss() }
    sure.setOnClickListener {
        show.dismiss()
        listener.onClick(it)
    }
}

fun getAddChildDialog(
    context: Context, listener: View.OnClickListener
) {
    val view = LayoutInflater.from(context).inflate(R.layout.dialog_add_child, null, false)
    val dialog = AlertDialog.Builder(context, R.style.dialog_radius_10).setCancelable(true)
    dialog.setView(view)
    val show = dialog.show()
    setDialogWindowAttr(show, context, 300f, 110f)
    val tv = view.findViewById<TextView>(R.id.dialog_add_child_tv)
    val btn = view.findViewById<TextView>(R.id.dialog_add_child_btn)
    tv.text = context.getString(R.string.this_need_add_child)
    btn.text = context.getString(R.string.go_to_add)
    btn.setOnClickListener {
        show.dismiss()
        listener.onClick(it)
    }
}

fun getBindIdentityDialog(
    context: Context, listener: View.OnClickListener
) {
    val view = LayoutInflater.from(context).inflate(R.layout.dialog_bind, null, false)
    val dialog = AlertDialog.Builder(context).setView(view).setCancelable(false)
    val show = dialog.show()
    setDialogWindowAttr(show, context, 275f, 288f)
    val sure = view.findViewById<TextView>(R.id.dialog_bind_sure)
    sure.setOnClickListener {
        show.dismiss()
        listener.onClick(it)
    }
}

fun getAdDialog(
    context: Context, data: AdvertisementModel, listener: View.OnClickListener
) {
    val view = LayoutInflater.from(context).inflate(R.layout.dialog_advertisement, null, false)
    val dialog = Dialog(context)
    dialog.setContentView(view)
    dialog.setCancelable(true)
    dialog.show()
    dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

    val iv = view.findViewById<ImageView>(R.id.dialog_advertisement_iv)
    GlideUtils.loadImage(context, data.imageUrl, iv, R.mipmap.no_data, R.mipmap.no_data)
    val sure = view.findViewById<TextView>(R.id.dialog_advertisement_sure)
    val cancel = view.findViewById<TextView>(R.id.dialog_advertisement_cancel)
    val noTip = view.findViewById<LinearLayout>(R.id.dialog_advertisement_no_tip)
    if (data.canHide == 1) {
        setDialogWindowAttr(dialog, context, 270f, 408f)
        noTip.visibility = View.VISIBLE
    } else {
        setDialogWindowAttr(dialog, context, 270f, 376f)
        noTip.visibility = View.GONE
    }
    val cb = view.findViewById<CheckBox>(R.id.dialog_advertisement_cb)
    sure.setOnClickListener {
        dialog.dismiss()
        it.tag = 1
        listener.onClick(it)
    }
    cancel.setOnClickListener {
        dialog.dismiss()
        if (cb.isChecked) {
            it.tag = 0
            listener.onClick(it)
        }
    }
}

fun getChangeToothBrushHeadDialog(
    context: Context, listener: View.OnClickListener
) {
    val view = LayoutInflater.from(context).inflate(R.layout.dialog_change_tooth_brush_head, null, false)
    val dialog = Dialog(context)
    dialog.setContentView(view)
    dialog.setCancelable(true)
    dialog.show()
    dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

    val sure = view.findViewById<TextView>(R.id.dialog_change_tooth_brush_head_sure)
    val cancel = view.findViewById<TextView>(R.id.dialog_change_tooth_brush_head_cancel)

    sure.setOnClickListener {
        dialog.dismiss()
        it.tag = 1
        listener.onClick(it)
    }
    cancel.setOnClickListener {
        dialog.dismiss()
        it.tag = 0
        listener.onClick(it)
    }
}

fun getFirstOpenDialog(
    context: Context, agreement1: View.OnClickListener, agreement2: View.OnClickListener, sureListener: View.OnClickListener, cancelListener: View.OnClickListener
) {
    val view = LayoutInflater.from(context).inflate(R.layout.dialog_first_open, null, false)
    val dialog = AlertDialog.Builder(context).setView(view).setCancelable(false)
    val show = dialog.show()
    setDialogWindowAttr(show, context, 295f, 400f)
    val tv: TextView = view.findViewById(R.id.dialog_first_open_tv)
    val sure: TextView = view.findViewById(R.id.dialog_first_open_agree)
    val cancel: TextView = view.findViewById(R.id.dialog_first_open_not_use)

    val string = "您可以随时在系统设置中关闭以上权限，请在使用小七泡泡前查看并同意完整的《隐私政策》和《用户服务协议》"
    val spannableBuilder = SpannableStringBuilder(string)
    val agreementClick1 = object : ClickableSpan() {
        override fun onClick(widget: View) {
            agreement1.onClick(widget)
        }

        override fun updateDrawState(ds: TextPaint) {
            ds.color = ContextCompat.getColor(context, R.color.mainTextColor)
            ds.isUnderlineText = false
        }
    }
    val agreementClick2 = object : ClickableSpan() {
        override fun onClick(widget: View) {
            agreement2.onClick(widget)
        }

        override fun updateDrawState(ds: TextPaint) {
            ds.color = ContextCompat.getColor(context, R.color.mainTextColor)
            ds.isUnderlineText = false
        }
    }
    spannableBuilder.setSpan(
        agreementClick1, string.length - 15, string.length - 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    spannableBuilder.setSpan(
        agreementClick2, string.length - 8, string.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    tv.movementMethod = LinkMovementMethod.getInstance()
    tv.text = spannableBuilder
    sure.setOnClickListener {
        show.dismiss()
        sureListener.onClick(it)
    }
    cancel.setOnClickListener {
        show.dismiss()
        cancelListener.onClick(it)
    }
}

//在dialog.show()之后调用
fun setDialogWindowAttr(dlg: Dialog, ctx: Context, width: Float, height: Float) {
    val window: Window = dlg.window!!
    val lp: WindowManager.LayoutParams = window.attributes
    lp.gravity = Gravity.CENTER
    lp.width = dp2px(ctx, width)
    lp.height = dp2px(ctx, height)
    dlg.window!!.attributes = lp
}

fun dp2px(context: Context, dpValue: Float?): Int {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue!!, context.resources.displayMetrics).toInt()
}